
var app = getApp()
var Bmob = require('../../utils/bmob.js');
var util = require('../../utils/util.js');
Page({
  data: {
    goodsInfo: [],
    submitData: {
      order_id: '',
      goods: [],
      score: 5,
      logistics_score: 5
    }
  },
  onLoad: function (options) {
    this.setData({
      'submitData.order_id': options.detail,
      'submitData.sub_shop_app_id': options.franchisee || ''
    })
    this.getOrderDetail();
  },
  getOrderDetail: function () {
    var that = this;
    var that = this;
    var orderlist = Bmob.Object.extend("orderList");
    var order = new Bmob.Query(orderlist);
    var currentUser = Bmob.User.current();
    var objectId = currentUser.id;
    var isme = new Bmob.User();
    isme.id = objectId;
    order.equalTo("users", isme);
    order.include("goodsInfo")
    order.get(that.data.submitData.order_id, {
      success: function (res) {

        var goodsType = res.attributes.goods_info.goods_type,
          goodsInfo = res.attributes.goods_info,
          goods = [];

        for (var i = 0, j = goodsInfo.length - 1; i <= j; i++) {
          goods.push({
            goods_id: goodsInfo[i].goods_id,
            info: {
              content: '',
              level: 1,
              img_arr: []
            }
          })
        }
        that.setData({
          goodsInfo: goodsInfo,
          'submitData.goods': goods,
          goodsType: goodsType
        })
      }
    })


  },
  setDescScore: function (e) {
    var score = e.target.dataset.score;
    this.setData({
      'submitData.score': score
    })
  },
  setLogisticsScore: function (e) {
    var score = e.target.dataset.score;
    this.setData({
      'submitData.logistics_score': score
    })
  },
  clickLevelSpan: function (e) {
    var goodsIndex = e.currentTarget.dataset.goodsIndex,
      level = e.currentTarget.dataset.level,
      data = {};

    data['submitData.goods[' + goodsIndex + '].info.level'] = level;
    this.setData(data);
  },
  commentInput: function (e) {
    var goodsIndex = e.target.dataset.goodsIndex,
      data = {};

    data['submitData.goods[' + goodsIndex + '].info.content'] = e.detail.value;
    this.setData(data);
  },
  chooseImage: function (e) {
    var that = this,
      goodsIndex = e.currentTarget.dataset.goodsIndex,
      img_arr = that.data.submitData.goods[goodsIndex].info.img_arr;

    if (img_arr.length >= 3) {
      app.showModal({
        content: '每个商品最多上传3张图片'
      });
      return;
    }

    app.chooseImage(function (images) {
      var data = {};
      data['submitData.goods[' + goodsIndex + '].info.img_arr'] = img_arr.concat(images);
      that.setData(data);
    }, 3 - img_arr.length);
  },
  removePic: function (e) {
    var goodsIndex = e.currentTarget.dataset.goodsIndex,
      picIndex = e.currentTarget.dataset.picIndex,
      img_arr = this.data.submitData.goods[goodsIndex].info.img_arr,
      data = {};

    img_arr.splice(picIndex, 1);
    data['submitData.goods[' + goodsIndex + '].info.img_arr'] = img_arr;
    this.setData(data);
  },
  makeComment: function () {
    var that = this,
      submitData = that.data.submitData,
      modalText = '';

    for (var i = submitData.goods.length - 1; i >= 0; i--) {
      var goods = submitData.goods[i];
      if (goods.info.content.length < 10) {
        modalText = '评价内容少于10个字';
        break;
      }
      if (!goods.info.level) {
        modalText = '尚未给商品评分';
        break;
      }
      if (goods.info.img_arr.length > 3) {
        modalText = '每个商品最多上传3张图片';
        break;
      }
    }

    if (modalText) {
      app.showModal({
        content: modalText
      })
      return;
    }

  
    var AssessList = Bmob.Object.extend("AssessList");
    var objects = new Array();
    for (var i = 0; i < that.data.submitData.goods.length; i++) {
      // 创建该类的一个实例
     
      var assessList = new AssessList();
      assessList.set("order_id", that.data.submitData.order_id);
      assessList.set("logistics_score", Number(that.data.submitData.logistics_score));
      assessList.set("score", Number(that.data.submitData.score));
      assessList.set("sub_shop_app_id", that.data.submitData.sub_shop_app_id);
      assessList.set("goods_id", that.data.submitData.goods[i].goods_id);
      assessList.set("content", that.data.submitData.goods[i].info.content);
      assessList.set("level", Number(that.data.submitData.goods[i].info.level));
      assessList.set("img_arr", that.data.submitData.goods[i].info.img_arr||[]);
      objects.push(assessList)
    }

    // 批量创建（更新）
    Bmob.Object.saveAll(objects).then(function (objects) {
      // 成功
      that.set_Order(that.data.submitData.order_id);
      console.log(objects);
      app.turnBack();
    },
      function (error) {
        // 异常处理
        console.log('创建日记失败');
        console.log(error);
      });
    

  },
  set_Order: function (id) {
    var that = this;
    var orderlist = Bmob.Object.extend("orderList");
    var order = new Bmob.Query(orderlist);
    order.get(id, {
      success: function (res) {
        res.set("status", 6);
        res.save();

      }

    })


  }
})
